using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TopMachine.Topping.Common.Interfaces;
using TopMachine.Topping.Common.Structures;
using TopMachine.Topping.Dawg;
using Topping.Core.Logic.Client;



namespace TopMachine.Topping.Engine.GUI.Board
{
    public partial class LettresRestantes : UserControl,ILettresRestantes
    {
        private GameCfg gc;
        private int _maxTop;


        public int MaxTop
        {
            get { return _maxTop; }
        }

        public GameCfg GameConfig
        {
            set { gc = value; }
        }

       

        private Label[] objPlacers;
        private Label[] objLabels;


        public Control ResultControl
        {
            get { return this; }
        }

        public LettresRestantes()
        {
           
            InitializeComponent();
        }

        public void SetConfig(GameCfg GameConfig)
        {
            gc = GameConfig; 
        }

        public LettresRestantes(GameCfg GameConfig)
        {
            InitializeComponent();
            gc = GameConfig; 
        }

        #region Letter BAG Management On Form


        public void ResetLettersTotal()
        {
            
                if (gc.Bag != null)
                {
                    for (int x = 0; x < gc.GridConfig.NbLetters; x++)
                    {
                        int tot = gc.Bag.isIn(x);
                        objLabels[x].Text = tot.ToString();

                        if (tot == 0)
                            objPlacers[x].Enabled = false;
                        else
                            objPlacers[x].Enabled = true;
                    }

                    lblLettres.Text = "Lettres Restantes : " + gc.Bag.ntiles().ToString();
                }
        }

        public void DisposeLetters()
        {
            if (objPlacers != null)
            {
                foreach (Label l in objPlacers)
                {
                    l.Dispose();
                }

                objPlacers = null;
            }

            if (objLabels != null)
            {
                foreach (Label l in objLabels)
                {
                    l.Dispose();
                }

                objLabels = null;
            }
        }

        public void ResetLetters()
        {
            try
            {
                if (gc == null || gc.Bag == null) return;

                CBag bag = gc.Bag;

                pnlLetters.Controls.Clear();

                DisposeLetters();
                objPlacers = new Label[gc.GridConfig.NbLetters];
                objLabels = new Label[gc.GridConfig.NbLetters];

                int nbLength = (this.Width - 20) / 35;

                if (this.Width < 100) return;

                _maxTop = 0;

                for (int x = 0; x < gc.GridConfig.NbLetters; x++)
                {
                    char c = gc.GridConfig.GetChar(x);

                    objLabels[x] = new Label();

                    int left, top, height, width;
                    string pts;

                    left = 10 + 35 * (x % nbLength);
                    top = 55 * (x / nbLength);
                    pts = gc.GridConfig.Values[x].ToString();

                    Label letterp = new Label();
                    letterp.Text = new string(c, 1);
                    letterp.Left = left;
                    letterp.Top = top;
                    letterp.Width = 30;
                    letterp.Height = 30;
                    letterp.BackColor = Color.DarkGray;
                    letterp.ForeColor = Color.White;
                    letterp.Font = new System.Drawing.Font(this.Font.FontFamily, 14, FontStyle.Bold);
                    letterp.TextAlign = ContentAlignment.MiddleCenter;

                    objLabels[x].Width = 30;
                    objLabels[x].Height = 18;
                    objLabels[x].Left = 10 + (35 * (x % nbLength));
                    objLabels[x].Top = 30 + 55 * (x / nbLength);
                    objLabels[x].BackColor = Color.SteelBlue;
                    letterp.TextAlign = ContentAlignment.BottomRight;

                    _maxTop = System.Math.Max(_maxTop, objLabels[x].Top + 23);
                    pnlLetters.Controls.Add(letterp);
                    pnlLetters.Controls.Add(objLabels[x]);
                    objPlacers[x] = letterp;
                }

                try
                {
                    this.Height = _maxTop + 25;
                }
                catch
                {
                    ;
                }
                ResetLettersTotal();
            }
            catch (Exception ee)
            {

                NLog.LogManager.GetLogger("logfile").Error("Error in LettresRestantes");
            }
        }

        #endregion

        private void LettresRestantes_Resize(object sender, EventArgs e)
        {
            ResetLetters();
        }

     
    }
}
